/* 
 * cms，一个基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department:运营部
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.service.dictionary.impl;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import org.grayrabbit.cms.entity.Dictionary;
/**
 * <b>OperLog</b>的service接口
 
 * @version 1.0,创建时间: 2011-05-17 14:46:47 
 */
public class DictionaryContext {
	
	public final static String JOIN_STRING = "$$"; 
	
	private static Map<String, List<Dictionary>> dicListMapById;
	private static Map<String, List<Dictionary>> dicListMapByCode;
	private static Map<String, List<Dictionary>> dicListMapByParent;
	private static Map<String,List<Dictionary>> dicParentListMap;
	private static Map<String, Dictionary> dicMapById;
	private static Map<String, Dictionary> dicMapByKey;
	public static boolean UPDATE_FLAG = true;	//更新标识
	
	public static boolean isUpdate(){
		return UPDATE_FLAG || dicListMapByCode==null || dicListMapByCode.isEmpty() || dicListMapByParent.isEmpty() ||dicParentListMap.isEmpty();
	}
	
	/**
	 * 应用级内存化字典信息
	 * @param dictionary
	 */
	public static void put(Dictionary dictionary){
		if(dictionary!=null){
			if(dictionary.getDictionaryType()!=null){
				if(dicListMapById==null){
					dicListMapById = new Hashtable<String, List<Dictionary>>();
				}
				if(dicMapById==null){
					dicMapById = new Hashtable<String, Dictionary>();
				}
				if(dicListMapByCode==null){
					dicListMapByCode = new Hashtable<String, List<Dictionary>>();
				}
				if(dicListMapByParent==null){
					dicListMapByParent = new Hashtable<String, List<Dictionary>>();
				}
				if(dicMapByKey==null){
					dicMapByKey = new Hashtable<String, Dictionary>();
				}
				if(dicParentListMap==null){
					dicParentListMap = new Hashtable<String, List<Dictionary>>();
				}
				if(dicParentListMap.get(dictionary.getDictionaryType().getDicTypeCode().toString())==null&&dictionary.getDictionary()!=null&&"-1".equals(dictionary.getDictionary().getDictionaryId())){
					dicParentListMap.put(dictionary.getDictionaryType().getDicTypeCode().toString(), new ArrayList<Dictionary>());
				}
				if(dictionary.getDictionary()!=null&&"-1".equals(dictionary.getDictionary().getDictionaryId())){
					dicParentListMap.get(dictionary.getDictionaryType().getDicTypeCode()).add(dictionary);
				}
				if(dicListMapById.get(dictionary.getDictionaryType().getDicTypeId().toString())==null){
					dicListMapById.put(dictionary.getDictionaryType().getDicTypeId().toString(), new ArrayList<Dictionary>());
				}
				dicListMapById.get(dictionary.getDictionaryType().getDicTypeId().toString()).add(dictionary);
				dicMapById.put(dictionary.getDictionaryId().toString(), dictionary);
				if(dicListMapByCode.get(dictionary.getDictionaryType().getDicTypeCode().toString())==null){
					dicListMapByCode.put(dictionary.getDictionaryType().getDicTypeCode().toString(), new ArrayList<Dictionary>());
				}
				dicListMapByCode.get(dictionary.getDictionaryType().getDicTypeCode()).add(dictionary);
				if(dictionary.getDictionary()!=null){
					if(dicListMapByParent.get(dictionary.getDictionary().getDictionaryId().toString())==null){
						dicListMapByParent.put(dictionary.getDictionary().getDictionaryId().toString(), new ArrayList<Dictionary>());
					}
					dicListMapByParent.get(dictionary.getDictionary().getDictionaryId()).add(dictionary);
				}
				dicMapByKey.put(dictionary.getDictionaryKey()+JOIN_STRING+dictionary.getDictionaryType().getDicTypeCode(), dictionary);
			}
		}
	}
	/**
	 * 根据字典类型ID获取字典信息
	 * @param dicTypeId
	 * @return
	 */
	public static List<Dictionary> getListByTypeId(String dicTypeId){
		return dicListMapById.get(dicTypeId);
	}
	/**
	 * 根据字典ID获取数据字典对象
	 * @param dicId
	 * @return
	 */
	public static Dictionary getByDicId(String dicId){
		return dicMapById.get(dicId);
	}
	/**
	 * 根据字典类型CODE获取字典信息
	 * @param dicTypeId
	 * @param dicId
	 * @return
	 */
	public static List<Dictionary> getListByCode(String dicTypeCode){
		if(dicListMapByCode!=null){
			return dicListMapByCode.get(dicTypeCode);
		}
		return new ArrayList<Dictionary>();
	}
	/**
	 * 根据字典父ID获取信息
	 * @param parentId
	 * @return
	 */
	public static List<Dictionary> getListByParent(String parentId){
		if(dicListMapByParent!=null){
			return dicListMapByParent.get(parentId);
		}
		return new ArrayList<Dictionary>();
	}
	
	public static List<Dictionary> getParentList(String dicTypeCode){
		if(dicParentListMap!=null){
			return dicParentListMap.get(dicTypeCode);
		}
		return new ArrayList<Dictionary>();
	}
	/**
	 * 根据数据字典KEY获取字典对象
	 * @param dicTypeId
	 * @param dicId
	 * @return
	 */
	public static Dictionary getByDicKey(String dicTypeKey,String dicTypeCode){
		return dicMapByKey.get(dicTypeKey+JOIN_STRING+dicTypeCode);
	}
	
	public static void remove(){
		if(dicListMapByCode != null)
			dicListMapByCode.clear();
		if(dicListMapById != null)
			dicListMapById.clear();
		if(dicMapById!=null)
			dicMapById.clear();
		if(dicMapByKey!=null)
			dicMapByKey.clear();
		if(dicListMapByParent!=null)
			dicListMapByParent.clear();
		if(dicParentListMap!=null)
			dicParentListMap.clear();
	}
}
